package com.quick.springbootquicky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.quick.springbootquicky.controller.ArticleController;
import com.quick.springbootquicky.mapper.ArticleMapper;
import com.quick.springbootquicky.page.PageBean;
import com.quick.springbootquicky.pojo.Article;
import com.quick.springbootquicky.service.ArticleService;
import com.quick.springbootquicky.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    /**
     * 添加新的文章
     * @param article
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Article article) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> pageList(Integer pageSize, Integer pageNum, Integer categoryId, String state) {
        //1、创建PageBean对象
        PageBean<Article> pageBean = new PageBean<>();
        //2、开启分页标志，引用mybatis的pageHelper
        PageHelper.startPage(pageNum, pageSize);
        //3、调用mapper
        //3.1、获取当前按用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId= (Integer) map.get("id");
        Article article = new Article();
        article.setCreateUser(userId);
        article.setCategoryId(categoryId);
        article.setState(state);
        List<Article> list = articleMapper.list(article);
        //3.2、Page中提供了方法，可以获取PageHelper分页查询后 得到的总记录数和当前页数据
        Page<Article> pg = (Page<Article>) list;
        //3.3、把数据填充到PageBean对象中
        pageBean.setTotal(pg.getTotal());
        pageBean.setItems(pg.getResult());
        return pageBean;
    }

    @Override
    public PageBean<Article> search(Integer pageSize, Integer pageNum, String categoryName, String state) {
        PageBean<Article> pageBean  = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map  = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Article article = new Article();
        article.setCreateUser(userId);
        article.setCategoryName(categoryName);
        article.setState(state);
        List<Article> list = articleMapper.list(article);
        //3.2、Page中提供了方法，可以获取PageHelper分页查询后 得到的总记录数和当前页数据
        Page<Article> pg = (Page<Article>) list;
        //3.3、把数据填充到PageBean对象中
        pageBean.setTotal(pg.getTotal());
        pageBean.setItems(pg.getResult());
        return pageBean;
    }

    /**
     * 更新文章
     * @param article
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Article article) {
        Map<String,Object> map =ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        article.setUpdateTime(new Date());
        articleMapper.update(article);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateState(Integer id, String state) {
        articleMapper.updateState(id,state);
    }
    /**
     * 根据id删除文章
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        articleMapper.delete(id);
    }

    /**
     * 根据文章分类id查询
     */
    @Override
    public List<Article> findByCategoryId(Integer categoryId) {
        Article article = new Article();
        article.setCategoryId(categoryId);
        return articleMapper.list(article);
    }
}
